package at.ac.lbg.media.vis.client.theme.render
{
	import at.ac.lbg.media.vis.client.theme.event.SelectCategoryEvent;
	import at.ac.lbg.media.vis.client.theme.view.Category;
	
	import flare.display.DirtySprite;
	
	import flash.events.MouseEvent;
	
	import mx.containers.HBox;
	import mx.controls.CheckBox;
	import mx.controls.Text;
	import mx.core.Application;
	import mx.core.ScrollPolicy;
	
	[Event(name="selectCategory", type="at.ac.lbg.media.vis.client.theme.event.SelectCategoryEvent")]

	public class CategoryRenderer extends HBox {
		private var cb:CheckBox;
		private var headline:Text;
		
		public function CategoryRenderer() {
			super();
			horizontalScrollPolicy = ScrollPolicy.OFF;
			
		}
		
		override protected function createChildren():void {
			cb = new CheckBox();
			cb.name = "cb";
			cb.addEventListener(MouseEvent.CLICK, handleCheck);
			addChild(cb);
			
			headline = new Text();
			headline.name = "headline";
			addChild(headline);
			
		}
		
		override protected function commitProperties():void {
			super.commitProperties();
			
			if (data) {
				cb.selected = data.isSelected;
//				cb.label = data.name;
				if (data.isMainCat) {
					headline.text = data.name;
				} else {
					headline.text = data.name + " (" + data.countArtworks + " %)";
				}
			}
		}
		
		override public function set data(value:Object):void {
			super.data = value;
			
			if (data) {
				if (data.isMainCat) {
					cb.visible = false;
					headline.setStyle("fontWeight", "bold");
				} else {
					cb.visible = true;
					headline.setStyle("fontWeight", "normal");
				}
			}
		}
		
		private function handleCheck(event:MouseEvent) : void {
			data.isSelected = !data.isSelected;
			Application.application.legendManager.handleSelectCategory(data as Category);
//			var e:SelectCategoryEvent = new SelectCategoryEvent("selectCategory");
//			e.cat = data as Category;
//			dispatchEvent(e);
			ArtworkRenderer.instance.stacks.removeAll();
			DirtySprite.renderDirty();
			Application.application.legendRenderer.redraw();
		}
		
	}
}